package com.huike.clues.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.huike.clues.domain.ClueTrackRecord;
import com.huike.clues.domain.ClueTrackRecordDto;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.service.ITbClueTrackRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2021-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService
{
    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;

    /**
     * 查询线索跟进记录列表
     *
     * @param tbClueTrackRecord 线索跟进记录
     * @return 线索跟进记录
     */
    @Override
    public List<TbClueTrackRecord> selectTbClueTrackRecordList(TbClueTrackRecord tbClueTrackRecord)
    {
        return tbClueTrackRecordMapper.selectTbClueTrackRecordList(tbClueTrackRecord);
    }

    /**
     * 修改线索跟进记录
     *
     * @param tbClueTrackRecord 线索跟进记录
     * @return 结果
     */
    @Override
    public int updateTbClueTrackRecord(TbClueTrackRecord tbClueTrackRecord)
    {
        return tbClueTrackRecordMapper.updateTbClueTrackRecord(tbClueTrackRecord);
    }

    /**
     * 批量删除线索跟进记录
     *
     * @param ids 需要删除的线索跟进记录ID
     * @return 结果
     */
    @Override
    public int deleteTbClueTrackRecordByIds(Long[] ids)
    {
        return tbClueTrackRecordMapper.deleteTbClueTrackRecordByIds(ids);
    }

    /**
     * 删除线索跟进记录信息
     *
     * @param id 线索跟进记录ID
     * @return 结果
     */
    @Override
    public int deleteTbClueTrackRecordById(Long id)
    {
        return tbClueTrackRecordMapper.deleteTbClueTrackRecordById(id);
    }

    /**
     * 添加跟进记录
     * @param clueTrackRecordVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addRecord(ClueTrackRecordVo clueTrackRecordVo) {
        Date date = new Date();
        String username = SecurityUtils.getUsername();

        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        BeanUtils.copyProperties(clueTrackRecordVo, clueTrackRecord);
        clueTrackRecord.setClueId(clueTrackRecordVo.getClueId().toString());
        clueTrackRecord.setCreateBy(username);
        clueTrackRecord.setCreateTime(date);
        clueTrackRecord.setType("0");

        // 添加线索跟进记录
        tbClueTrackRecordMapper.addRecord(clueTrackRecord);

        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(clueTrackRecordVo, tbClue);
        tbClue.setId(clueTrackRecordVo.getClueId());
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());

        // 根据线索id修改线索
        tbClueMapper.updateTbClue(tbClue);
    }

    /**
     * 查询线索跟进记录列表
     * @param clueId
     * @return
     */
    // @Override
    // public ClueTrackRecordDto list(Long clueId) {
    //     List<ClueTrackRecord> clueTrackRecords = tbClueTrackRecordMapper.list(clueId);
    //
    //     ClueTrackRecordDto clueTrackRecordDto = new ClueTrackRecordDto();
    //     clueTrackRecordDto.setRows(clueTrackRecords);
    //     clueTrackRecordDto.setTotal(clueTrackRecords.size());
    //     clueTrackRecordDto.setCode(200);
    //     clueTrackRecordDto.setMsg("查询成功");
    //     return clueTrackRecordDto;
    // }

    /**
     * 查询线索跟进记录列表
     * @param clueId
     * @return
     */
    @Override
    public TableDataInfo findByList(Long clueId) {

        TableDataInfo tableDataInfo = new TableDataInfo();

        //查询rows集合
        List<TbClueTrackRecord> list = tbClueTrackRecordMapper.findByList(clueId);


        //线索跟进的总数
        tableDataInfo.setTotal(list.size());
        //封装rows
        tableDataInfo.setRows(list);
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");

        tableDataInfo.setParams(null);
        return tableDataInfo;
    }
}
